//
//  GameRootViewController.m
//  gamebox_qpp
//
//  Created by 赵隆杰 on 2021/12/10.
//

#import "GameRootViewController.h"
#import "GameLeftTableViewCell.h"
#import "GameRightTableViewCell.h"

#import "GameTypeListApi.h"
#import "GameListApi.h"

#import "GameModel.h"

#import "GameDetailViewController.h"

static float kLeftTableViewWidth = 83.f;
@interface GameRootViewController ()

@property(nonatomic,strong) UITableView *leftTableView;
@property(nonatomic,strong) UITableView *rightTableView;

@property (nonatomic, strong) GameListApi *listApi;

@property (nonatomic, strong) NSMutableArray<GameTypeModel *> *types;
@property (nonatomic, strong) NSMutableArray<GameModel *> *rightDataAry;

@property(nonatomic) BOOL firstLoad; //区分是否是第一次加载来选中table的第一行
@property(nonatomic, strong) GameTypeModel *currentfirstModel;
@end

@implementation GameRootViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.centerTitle.text = @"游戏库";
    
    [self.view addSubview:self.leftTableView];
    [self.view addSubview:self.rightTableView];

    GameTypeListApi *api = [[GameTypeListApi alloc] init];
    [api startWithCompletionBlockWithSuccess:^(__kindof YTKBaseRequest * _Nonnull request) {
        if (api.success) {
            self.types = [GameTypeModel mj_objectArrayWithKeyValuesArray:api.res];
            NSIndexPath *path = [NSIndexPath indexPathForRow:0 inSection:0];
            [self tableView:self.leftTableView didSelectRowAtIndexPath:path];
            self.firstLoad = YES;
            [self.leftTableView reloadData];
        }
    } failure:^(__kindof YTKBaseRequest * _Nonnull request) {
        NSLog(@"");
    }];
    
}

#pragma mark =================== UITableViewDelegate========================
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView == self.leftTableView) {
        return self.types.count;
    }
    return self.rightDataAry.count;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == self.leftTableView) {
        GameLeftTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:k_GameCellIdentifier_Left forIndexPath:indexPath];
        if (indexPath.row == 0 && self.firstLoad == YES)
        {
            [tableView selectRowAtIndexPath:indexPath
                                   animated:NO
                             scrollPosition:UITableViewScrollPositionNone];
        }
        self.firstLoad = NO;
        cell.indexPath = indexPath;
        GameTypeModel *cateGory = [self.types objectAtIndex:indexPath.row];
        cell.name.text = cateGory.gameName;
        return cell;
    }
    GameRightTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:k_GameCellIdentifier_Right forIndexPath:indexPath];
    GameModel *gameModel = [self.rightDataAry objectAtIndex:indexPath.row];
    cell.gameModel = gameModel;
    return cell;
    
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == self.leftTableView) {
        GameTypeModel *cateGory = [self.types objectAtIndex:indexPath.row];
        self.currentfirstModel = cateGory;
        [self getRightTableViewAry];
    } else {
        GameModel *model = self.rightDataAry[indexPath.row];
        GameDetailViewController *vc = [[GameDetailViewController alloc] init];
        vc.modelId = @"1";
        [self.navigationController pushViewController:vc animated:YES];
    }
    
  
}

-(void)getRightTableViewAry
{
    self.listApi.typeId = self.currentfirstModel.gameTypeId;
    [self.listApi startWithCompletionBlockWithSuccess:^(__kindof YTKBaseRequest * _Nonnull request) {
        [self.rightTableView.mj_header endRefreshing];
        if (self.listApi.success) {
            if (self.listApi.pageNum == 1) {
                [self.rightDataAry removeAllObjects];
            }
            NSArray *dataArray = [GameModel mj_objectArrayWithKeyValuesArray:self.listApi.resArr];
            [self.rightDataAry addObjectsFromArray:dataArray];
            [self.rightTableView reloadData];
        }
        if (self.listApi.isMore) {
            [self.rightTableView.mj_footer endRefreshing];
        } else {
            [self.rightTableView.mj_footer endRefreshingWithNoMoreData];
        }
    } failure:^(__kindof YTKBaseRequest * _Nonnull request) {
        [self.rightTableView.mj_footer endRefreshing];
        [self.rightTableView.mj_header endRefreshing];
    }];
}

#pragma mark - 懒加载

- (NSMutableArray<GameTypeModel *> *)types
{
    if (!_types) {
        _types = [[NSMutableArray<GameTypeModel *> alloc] init];
    }
    return _types;
}

- (NSMutableArray<GameModel *> *)rightDataAry
{
    if (!_rightDataAry)
    {
        _rightDataAry = [NSMutableArray<GameModel *> array];
    }
    return _rightDataAry;
}


- (UITableView *)leftTableView
{
    if (!_leftTableView)
    {
        _leftTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, UIDevice.vg_navigationFullHeight, kLeftTableViewWidth, SL_WINDOW_HEIGHT - UIDevice.vg_navigationFullHeight - UIDevice.vg_tabBarFullHeight)];
        _leftTableView.delegate = self;
        _leftTableView.dataSource = self;
//        _leftTableView.tableFooterView = [UIView new];
        _leftTableView.rowHeight = 48;
        _leftTableView.showsVerticalScrollIndicator = NO;
        _leftTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_leftTableView registerClass:[GameLeftTableViewCell class] forCellReuseIdentifier:k_GameCellIdentifier_Left];
        
        UILabel *rightLongLine = [[UILabel alloc] initWithFrame:CGRectMake(kLeftTableViewWidth-1, UIDevice.vg_navigationFullHeight, 1, _leftTableView.height)];
        rightLongLine.backgroundColor = kLineColor;
        [self.view addSubview:rightLongLine];
    }
    return _leftTableView;
}

- (UITableView *)rightTableView
{
    if (!_rightTableView)
    {
        _rightTableView = [[UITableView alloc] initWithFrame:CGRectMake(kLeftTableViewWidth, UIDevice.vg_navigationFullHeight, SL_WINDOW_WIDTH - kLeftTableViewWidth, SL_WINDOW_HEIGHT - UIDevice.vg_navigationFullHeight - UIDevice.vg_tabBarFullHeight)];
        _rightTableView.backgroundColor = kViewF6Color;
        _rightTableView.delegate = self;
        _rightTableView.dataSource = self;
//        _rightTableView.tableFooterView = [UIView new];
        _rightTableView.rowHeight = 70;
        _rightTableView.showsVerticalScrollIndicator = NO;
        _rightTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_rightTableView registerClass:[GameRightTableViewCell class] forCellReuseIdentifier:k_GameCellIdentifier_Right];
    }
    return _rightTableView;
}

- (GameListApi *)listApi
{
    if (!_listApi) {
        _listApi = [[GameListApi alloc] init];
    }
    return _listApi;
}



@end
